package ylsb.support;

import ylsb.dao.YltpJieguoMapper;
import ylsb.model.YltpJieguo;

import java.util.List;
import java.util.concurrent.CountDownLatch;

public class BatchInsertBankRecordThread implements Runnable {

    private YltpJieguoMapper yltpJieguoMapper;
    /**
     * 数据集合
     */
    private List<YltpJieguo> list;
    /**
     * 每个线程处理的起始数据
     */
    private CountDownLatch begin;
    /**
     * 每个线程处理的结束数据
     */
    private CountDownLatch end;

    public BatchInsertBankRecordThread(List<YltpJieguo> list,
                                       CountDownLatch begin,
                                       CountDownLatch end,
                                       YltpJieguoMapper yltpJieguoMapper) {
        this.list = list;
        this.begin = begin;
        this.end = end;
        this.yltpJieguoMapper = yltpJieguoMapper;
    }

    @Override
    public void run() {
        try {
            if (list != null && !list.isEmpty()) {
                yltpJieguoMapper.insertBatch(list);
            }
            // 执行完让线程直接进入等待
            begin.await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 当一个线程执行完 了计数要减一不然这个线程会被一直挂起
            end.countDown();
        }
    }

}

